<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\DianpuRequest;
class DianpuController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {   
        
        $data=$request->only('num','keywords');

        if(empty($data['num']))
        {
            $data['num']=10;
        }
        
        if(empty($data['keywords'])){
        $res=DB::table('sdianpu')->leftJoin('users','sdianpu.uid','=','users.id')->select('sdianpu.name','sdianpu.logo','users.name as uname','users.phone','users.email','sdianpu.id','sdianpu.status')->paginate($data['num']);
        }else{
            $keywords=$request->input('keywords');
            $res=DB::table('sdianpu')->leftJoin('users','sdianpu.uid','=','users.id')->select('sdianpu.name','sdianpu.logo','users.name as uname','users.phone','users.email','sdianpu.id','sdianpu.status')->where('sdianpu.name','like','%'.$keywords.'%')->paginate($data['num']);
        }

        return view('Admin.dianpu.index',['res'=>$res,'data'=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getCreate()
    {
        return view('Admin.dianpu.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postAdd(DianpuRequest $request)
    {
        $data=$request->only('name','logo','address','zhizhao','detail');
        //dd($data);

        $tu=$request->only('logo','zhizhao');

        foreach ($tu as $k => $v) 
            {   
                $data[$k]=$this->upload($request,$k);    
            }

        

        if(!$data['logo'])
        {
            return redirect('admin/dianpu/create')->with('error','请上传图片');
        }

        $shophost=$request->input('shophost');
        $uid=DB::table('users')->where('name',$shophost)->select('id')->first();
        
        if(!$uid){
            return redirect('admin/dianpu/create')->with('error','此用户不存在');
        }

        $data['uid']=$uid->id;
        // dd($uid);

        $res=DB::table('sdianpu')->insertGetId($data);
        
        if($res)
        {
            return redirect('admin/dianpu/index')->with('success','添加成功');
        }else{
            return redirect('admin/dianpu/create')->with('error','添加失败');
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function upload($request,$k)
    {
        
        if($request->hasFile($k))
        {
            $name=md5($request[$k]).time();
            
            $su=$request->file($k)->getClientOriginalExtension();
            $request->file($k)->move('./public/uploads/dianpu',$name.'.'.$su);
            return '/public/uploads/dianpu/'.$name.'.'.$su;
        }

        
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getDel($id)
    {        
       

       $res=DB::table('shangpin')->where('shopid',$id)->get();
       
       if($res)
       {
            $res1=DB::table('shangpin')->where('shopid',$id)->delete();

            if($res1)
            {
                $res2=DB::table('sdianpu')->where('id',$id)->delete();

                if($res2)
                {
                    return redirect('admin/dianpu/index');
                }else{
                    echo '失败';
                }
            }else{
                echo '失败';
            }
       }else{
            $res1=DB::table('sdianpu')->where('id',$id)->delete();
            
            if($res1)
            {
                return redirect('admin/dianpu/index');
            }else{
                echo '失败';
            }
       }
   }
    public function getSelect($id)
    {
        $res=DB::table('sdianpu')->leftJoin('users','sdianpu.uid','=','users.id')->where('sdianpu.id',$id)->select('sdianpu.name','sdianpu.logo','sdianpu.address','users.name as uname','users.phone','users.email','sdianpu.id','users.photo','users.email','sdianpu.detail','sdianpu.zhizhao')->first();
        
        return view('admin/dianpu/select',['res'=>$res]);

    }
}
